package com.vercon.commonmirror.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import butterknife.ButterKnife;
import butterknife.Unbinder;


import com.vercon.commonmirror.utils.LogUtils;


/**
 * Created by csh on 2021/1/22
 * CommonMirror公共资源文件，所有公共调用的资源都放这里
 */
public abstract class BaseActivity extends FragmentActivity {

    @SuppressLint("StaticFieldLeak")
    private static Context mContext;
    private Unbinder mUnbinder;
    private boolean isHideKeyboard = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化界面
        setContentView(setContentView(savedInstanceState));
        mUnbinder = ButterKnife.bind(this);
        mContext = getApplicationContext();
        //初始化
        initView();
        //初始化监听
        initListener();
    }

    public static Context getContext() {
        return mContext;
    }

    public void setHideKeyboard(boolean isHideKeyboard) {
        this.isHideKeyboard = isHideKeyboard;
    }

    @Override
    protected void onResume() {
        super.onResume();
//        hideBottomMenu();
    }

    /**
     * startActivity
     *
     * @param context
     * @param tClass
     * @param <T>
     */
    public static <T> void start(Context context, Class<T> tClass) {
        Intent i = new Intent(context, tClass);
        context.startActivity(i);
    }

    protected abstract int setContentView(@Nullable Bundle savedInstanceState);

    protected abstract void initView();

    protected abstract void initListener();


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideKeyboard(v, ev) && isHideKeyboard) {
                hideKeyboard(v);
            }
        }

        return super.dispatchTouchEvent(ev);
    }

    private void hideBottomMenu() {
        //全屏
        //for new api versions.
        //A99使用会注销系统（重启）
//        View decorView = getWindow().getDecorView();
//        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
//                | View.SYSTEM_UI_FLAG_FULLSCREEN;
//        decorView.setSystemUiVisibility(uiOptions);

    }


    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v instanceof EditText) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left + v.getWidth();
            // 点击EditText的事件，忽略它。
            return !(event.getX() > left) || !(event.getX() < right) || !(event.getY() > top) || !(event.getY() < bottom);
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上，和用户用轨迹球选择其他的焦点
        return false;
    }

    public void hideKeyboard(View view) {
        //隐藏键盘
        if (view != null) {
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            if (im != null) {
                im.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
//        hideBottomMenu();
    }

    public void showKeyboard(View view) {
        //显示键盘
        InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (im != null) {
            view.requestFocus();
            im.showSoftInput(view, 0);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mContext = null;
        mUnbinder.unbind();//在页面销毁时解绑
        LogUtils.d("onDestroy");

    }
}